home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Apple Developer Connection Student Program
/
ADC Tools Sampler CD Disk 3 1999.iso
/
Documentation
/
Books
/
Learn Java on the Macintosh
/
Learn Java Projects
/
12.02 - arrays
/
ArrayApplet.java
< prev
next >
Wrap
Text File
|
1996-04-22
|
1KB
|
41 lines
/* -------------------------------------------------------------
This applet displays your fortune whenever you resize the applet.
Java's classes: Applet (applet)
Graphics (awt) used for drawing
Math (lang) to find the absoluate value
Date (util) gets the current date
Random (util) finds a random number
Custom classes: ArrayApplet
------------------------------------------------------------- */
import java.awt.Graphics;
import java.util.Date;
import java.util.Random;
public class ArrayApplet extends java.applet.Applet {
int numStrings = 5;
String[] paintStrings;
Random r;
public void init() {
Date d = new Date(); // today's date
r = new Random(d.getTime()); // seed with milliseconds since 1970
paintStrings = new String[numStrings];
paintStrings[0] = new String("Look for opportunities");
paintStrings[1] = new String("Take chances");
paintStrings[2] = new String("Beware of tricks");
paintStrings[3] = new String("Take the day off");
paintStrings[4] = new String("Smell the roses");
}
public void paint(Graphics g) {
int index = r.nextInt() % numStrings;
index = Math.abs(index);
g.drawString(paintStrings[index], 50, 25);
}
}